/**
 *  监听事件的处理类，兼容IE系列
 * @param obj dom对象
 * @param eventName 事件名,带on开头的，例如onclick
 * @param fn function对象
 */
var EventListener = (function() {
	var _ieBindFun = [];//为了后面解绑事件监听，需要维护一个事件数组
	return {
		addEventHandler: function(obj, eventName, fn,useCapture) {
			if (obj.addEventListener) {
				obj.addEventListener(eventName.substring(2, eventName.length), fn, useCapture ? useCapture : false);
			} else if(obj.attachEvent) {
				var _fun = function(event) {//解决IE10以下的this指向问题
					fn.call(obj, event);
				}
				_ieBindFun[obj] = _ieBindFun[obj] || [];
				_ieBindFun[obj].push({ eventName: eventName, func: _fun });
				obj.attachEvent(eventName, _fun);
			} else {
				obj[eventName] = fn;
			}
		},
		removeEventHandler: function(obj, eventName, fn,useCapture) {
			if(obj.removeEventListener) {
				obj.removeEventListener(eventName.substring(2, eventName.length), fn, useCapture ? useCapture : false);
			} else if(obj.detachEvent) {
				var eventObjs = _ieBindFun[obj];
				if(eventObjs != undefined) {
					for(var i = 0; i < eventObjs.length; i++) {
						var eventObj = eventObjs[i];
						if(eventObj.eventName == eventName) {
							obj.detachEvent(eventName, eventObj.func);
						}
					}
				}
			} else {
				delete obj[eventName];
			}
		}
	}
})();

module.exports = EventListener;